package com.zoo.diannaopeijian.controller;

import com.github.pagehelper.PageInfo;
import com.zoo.diannaopeijian.comm.page.Page;
import com.zoo.diannaopeijian.domain.Stock;
import com.zoo.diannaopeijian.domain.Ware;
import com.zoo.diannaopeijian.domain.Warehouse;
import com.zoo.diannaopeijian.service.StockService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zx583 on 2018-01-27.
 */
@Controller
@RequestMapping("/stock")
public class StockController extends BaseController {
    @Resource
    private StockService stockService;

    @RequestMapping("/stockManaPage")
    public String stockManaPageRoute() {
        return "stock/stockManaPage";
    }

    @RequestMapping("/getStock")
    @ResponseBody
    public Map getStock(Page page,
                        @RequestParam(required = true, defaultValue = "false") boolean isFuzzy,
                        Integer componentid,
                        Integer componentparameterdetailid,
                        Integer warehouseid,
                        Ware ware) {
        Map<String, Object> result = new HashMap<>();

        List datas = isFuzzy ? stockService.getFuzzyStockWithPage(page, componentid, componentparameterdetailid, warehouseid, ware) : stockService.getWareStockWithPage(page, componentid, componentparameterdetailid, warehouseid, ware);

        result.put(rowsWord, datas);
        result.put(totalWord, new PageInfo<>(datas).getTotal());

        return result;
    }

    @RequestMapping("/getStockNum")
    @ResponseBody
    public Integer getStockNum(Stock stock) {
        return stockService.getStockNum(stock);
    }

    @RequestMapping("/updateStock")
    @ResponseBody
    public String updateStock(Stock stock) {
        return stockService.updateStock(stock) ? success : fail;
    }
}
